<template>
  <div class="page">
    <el-form label-width="80px" inline :model="searchFrom">
      <el-form-item label="部位名称">
        <el-input v-model="searchFrom.name" />
      </el-form-item>
      <el-form-item label="启用状态">
        <el-select v-model="searchFrom.status" placeholder="请选择启用状态" clearable style="width:100%">
          <el-option label="启用" :value="0" />
          <el-option label="禁用" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="getPart">搜索</el-button>
        <el-button @click="show()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" border>
      <el-table-column prop="name" label="部位名称" width="" />
      <el-table-column prop="status" label="启用状态" width="">
        <template slot-scope="{row}">
          {{ row.status === 0 ? '启用' : '禁用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="{row}">
          <el-button type="text" @click="show(row)">修改</el-button>
          <el-button type="text" style="color: #F56C6C;" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination :current-page.sync="page.pageNo" :page-size="page.pageSize" layout="total, prev, pager, next"
      :total="page.total" @size-change="pageSizeChange" @current-change="pageNoChange" />

    <el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="部位名称">
          <el-input v-model="form.name" placeholder="请填写症状" />
        </el-form-item>
        <el-form-item label="启用状态">
          <el-select v-model="form.status" placeholder="请选择启用状态" style="width:100%">
            <el-option label="启用" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getPart, partAdd, partUpdate, partDelete } from '@/api/official/official'
export default {
  name: 'InternetHospitalMangeSmartGuide',

  data() {
    return {
      searchFrom: {},
      page: {
        total: 0,
        pageSize: 10,
        pageNo: 1
      },
      tableData: [],
      form: {},
      dialogVisible: false,
      loading: false
    }
  },

  mounted() {
    this.getPart()
  },

  methods: {
    getPart() {
      getPart({ size: 10, ...this.searchFrom }).then(res => {
        this.tableData = res.records
        this.page.total = res.total
      })
    },
    pageSizeChange(val) {
      this.page.pageSize = val
      this.getPart()
    },
    pageNoChange(val) {
      this.page.pageNo = val
      this.getPart()
    },
    show(row = {}) {
      this.dialogVisible = true
      this.form = { ...row }
    },
    del(id) {
      partDelete({ ids: [id] }).then(res => {
        this.$message.success('删除成功')
        this.getPart()
      })
    },
    submit() {
      this.loading = true
      const data = {
        ...this.form
      }
      if (this.form.id) {
        partUpdate(data).then(res => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getPart()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      } else {
        partAdd(data).then(res => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.getPart()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  padding: 24px;
}
</style>
